/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2012 All Rights Reserved.
 */
package com.pingan.mobilegw.mobileservice.invoke;

import com.pingan.mobilegw.adapterservice.model.CacheSession;
import com.pingan.mobilegw.adapterservice.model.MapOperation;
import com.pingan.mobilegw.adapterservice.model.MobilegwConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * 无线RPC参数Holder
 * 
 * @author jiadao
 * @version $Id: MobileRpcHolder.java, v 0.1 2013-4-12 下午3:17:05 jiadao Exp $
 */
public class MobileRpcHolder {

    /** 无线session_key */
    public static final String                      SESSION_KEY = "mobilegw_session";

    /** 无线cookie_key */
    public static final String                      COOKIE_KEY  = "mobilegw_cookie";

    /** 无线header_key */
    public static final String                      HEADER_KEY  = "mobilegw_header";

    /** remote_addr */
    public static final String                      REMOTE_ADDR = "remote_addr";

    /** user-agent */
    public static final String                      USER_AGENT  = "user-agent";

    /** ThreadLocal */
    private static ThreadLocal<Map<Object, Object>> tlmap       = new ThreadLocal<Map<Object, Object>>() {
                                                                    /**
                                                                     * 默认返回一个空map
                                                                     */
                                                                    public Map<Object, Object> initialValue() {
                                                                        return new HashMap<Object, Object>();
                                                                    }
                                                                };

    /**
     * 从threadLocal中获取对象
     * 
     * @param key
     * @return
     */
    public static Object get(Object key) {
        return tlmap.get().get(key);
    }

    /**
     * 设置ThreadLocal
     * 
     * @param key 务必加上系统前缀或后缀
     * @param value
     * @return
     */
    public static Object put(Object key, Object value) {
        return tlmap.get().put(key, value);
    }

    /**
     * 获取无线Session
     * 
     * @return
     */
    public static CacheSession getSession() {
        return (CacheSession) get(SESSION_KEY);
    }

    /**
     * 设置无线session
     * 
     * @param session
     */
    public static void setSession(CacheSession session) {
        put(SESSION_KEY, session);
    }

    /**
     * 获得header信息
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> getHeaders() {
        return (Map<String, String>) get(HEADER_KEY);
    }

    /**
     * 设置header信息
     * 
     * @param headers
     */
    public static void setHeaders(Map<String, String> headers) {
        put(HEADER_KEY, headers);
    }

    /**
     * 获取remote_addr客户端ip
     * 
     * @return
     */
    public static String getRemoteAddr() {
        return getHeaders().get(REMOTE_ADDR);
    }

    /**
     * 获取客户端user-agent
     * 
     * @return
     */
    public static String getUserAgent() {
        return getHeaders().get(USER_AGENT);
    }

    /**
     * 获取增量数据变量
     * <b>业务系统忽略该接口</b>
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, MapOperation> getIncrementalChanges() {
        Map<String, MapOperation> map = (Map<String, MapOperation>) get(MobilegwConstant.INCREMENTAL_CHANGES);
        if (map == null) {
            map = new HashMap<String, MapOperation>();
            put(MobilegwConstant.INCREMENTAL_CHANGES, map);
        }
        return map;
    }

    /**
     * 清理ThreadLocal
     */
    public static void clear() {
        tlmap.remove();
    }

}
